import LevelBaseControl from "./LevelBaseControl";
import { utils } from "../../utils/CommonUtil";

/** 交互物体基类*/
export default class InteractiveBase extends Laya.Script3D {
    /** 父交互物体*/
    public parentInteractive: InteractiveBase;
    /** 是否需要先调用本关卡脚本逻辑判断 在进行交互操作*/
    public isNeedCallToLevel: boolean = false;
    /** 执行交互逻辑 返回bool值（如果为true  而且是从子交互物体调过来的 那么子交互物体就不会执行自身的交互逻辑了 InteractiveLogic行
     * 如果继续点   福交互物体不会再聚焦了 返回false  柜子门就会执行自身的逻辑 打开柜子门）*/
    public DoInteractive(): boolean {
        if(this.owner.name == "Box034"|| this.owner.name == "passWordLock"){
            
            console.log(this.owner.name,this.parentInteractive);
        }

        /** 先执行父交互物体的行为 如果父交互物体有行为要做  就暂时不做自己的*/
        if (this.parentInteractive != null && this.parentInteractive.DoInteractive()) {
            return true;
        }
        else {
            /** 限制了操作*/
            if (this.isNeedCallToLevel && !this.CallToLevel()) {
                return true;
            }
            else {
                /** 执行自身的交互逻辑*/
                return this.InteractiveLogic();
            }

        }
    }
    /** 点击交互逻辑*/
    protected InteractiveLogic(): boolean {
        return true;
    }

    public SetParent(parentInteractive: InteractiveBase): void {
        if( this.owner.name == "passWordLock"){
            console.log(this.owner.name ,parentInteractive)
        }
        if (this.parentInteractive != null) {
            if (this.parentInteractive.parentInteractive == parentInteractive) {
                return;
            }
        }
        if(parentInteractive != null){
            this.parentInteractive = parentInteractive;
        }
    }
    /** 遍历所有子交互物体 设置其父交互物体尾自身*/
    public SetAllSonObj(): void {
      
        let allSonObjs: Array<InteractiveBase> = new Array<InteractiveBase>();
        utils.CommonUtils.GetComponentsInChildren<InteractiveBase>(this.owner as Laya.Sprite3D,InteractiveBase,allSonObjs);
        if(this.owner.name == "gateLockFocus"||this.owner.name == "passWordLock"){
            
            console.log(this.owner.name,allSonObjs);
        }
        let length: number = allSonObjs.length;
        for (let i = 0; i < length; i++) {
            let sonObj: InteractiveBase = allSonObjs[i];
            /** 自己不算*/
            if (sonObj.owner == this.owner) {
                continue;
            }
            else {
                sonObj.SetParent(this);
            }
        }
    }

    private GetChildComponents(node: Laya.Node): Array<InteractiveBase> {
        let childCount = node.numChildren;
        let components = new Array<InteractiveBase>();
        for (let index = 0; index < childCount; index++) {
            let child = node.getChildAt(index);
            let component = child.getComponent(InteractiveBase) as InteractiveBase;
            if (component != null) {
                components.push(component);
            }
        }

        return components;
    }

    /** 调用本关卡逻辑方法进行相应的处理 传递此交互物体名字*/
    public CallToLevel(str: string = null): boolean {
        if (str == null) {
            /** 默认传节点名字*/
            str = this.owner.name;
        }
        let levelControl: LevelBaseControl = window.gameFacade.LevelMng.GetCurLevelControl();
        return levelControl.DoInteractObjLogic(str);
    }
}